<template>
  <div class="courses-container">
    <el-card>
      <template #header>
        <div class="card-header">
          <h2>课程列表</h2>
          <div class="header-actions">
            <el-radio-group v-model="category" size="small" @change="handleCategoryChange">
              <el-radio-button label="all">全部</el-radio-button>
              <el-radio-button label="frontend">前端开发</el-radio-button>
              <el-radio-button label="backend">后端开发</el-radio-button>
              <el-radio-button label="mobile">移动开发</el-radio-button>
              <el-radio-button label="ai">人工智能</el-radio-button>
            </el-radio-group>

            <el-select v-model="sortBy" placeholder="排序方式" size="small">
              <el-option label="最新" value="latest" />
              <el-option label="最热" value="hot" />
              <el-option label="评分最高" value="rating" />
              <el-option label="价格最低" value="price-asc" />
              <el-option label="价格最高" value="price-desc" />
            </el-select>
          </div>
        </div>
      </template>

      <div class="course-list">
        <el-card
          v-for="course in courses"
          :key="course.id"
          class="course-card"
          @click="handleCourseClick(course)"
        >
          <div class="course-cover">
            <el-image :src="course.cover" fit="cover" />
            <div class="course-tag" v-if="course.isNew">新课</div>
            <div class="course-tag hot" v-if="course.isHot">热门</div>
          </div>

          <div class="course-info">
            <h3 class="course-title">{{ course.title }}</h3>
            <p class="course-desc">{{ course.description }}</p>

            <div class="course-meta">
              <div class="teacher">
                <el-avatar :size="24" :src="course.teacher.avatar" />
                <span>{{ course.teacher.name }}</span>
              </div>

              <div class="stats">
                <span>
                  <el-icon><User /></el-icon>
                  {{ course.studentCount }}人学习
                </span>
                <span>
                  <el-icon><Star /></el-icon>
                  {{ course.rating }}分
                </span>
              </div>
            </div>

            <div class="course-footer">
              <div class="tags">
                <el-tag
                  v-for="tag in course.tags"
                  :key="tag"
                  size="small"
                >
                  {{ tag }}
                </el-tag>
              </div>
              <div class="price">
                <span class="current-price">¥{{ course.price }}</span>
                <span class="original-price" v-if="course.originalPrice">¥{{ course.originalPrice }}</span>
              </div>
            </div>
          </div>
        </el-card>
      </div>

      <div class="pagination">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :total="total"
          :page-sizes="[12, 24, 36, 48]"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import {
  User,
  Star
} from '@element-plus/icons-vue'

const router = useRouter()

// 分类筛选
const category = ref('all')

// 排序方式
const sortBy = ref('latest')

// 分页
const currentPage = ref(1)
const pageSize = ref(12)
const total = ref(100)

// 课程数据
const courses = ref([
  {
    id: 1,
    title: 'Vue.js 3.0 实战教程',
    description: '从入门到精通的 Vue.js 3.0 课程，包含组合式 API、TypeScript 等新特性',
    cover: 'https://example.com/course1.jpg',
    isNew: true,
    isHot: true,
    teacher: {
      name: '张老师',
      avatar: 'https://example.com/avatar1.jpg'
    },
    studentCount: 5000,
    rating: 4.8,
    tags: ['Vue.js', 'TypeScript', '前端开发'],
    price: 299,
    originalPrice: 399
  },
  {
    id: 2,
    title: 'React 高级开发实战',
    description: '深入理解 React 核心概念，掌握高级开发技巧，成为 React 技术专家',
    cover: 'https://example.com/course2.jpg',
    isNew: false,
    isHot: true,
    teacher: {
      name: '李老师',
      avatar: 'https://example.com/avatar2.jpg'
    },
    studentCount: 3000,
    rating: 4.9,
    tags: ['React', 'Redux', '前端开发'],
    price: 399,
    originalPrice: 499
  }
])

// 切换分类
const handleCategoryChange = () => {
  currentPage.value = 1
  fetchCourses()
}

// 获取课程列表
const fetchCourses = () => {
  // TODO: 根据分类、排序和分页获取数据
}

// 切换页码
const handleCurrentChange = (page) => {
  currentPage.value = page
  fetchCourses()
}

// 切换每页条数
const handleSizeChange = (size) => {
  pageSize.value = size
  currentPage.value = 1
  fetchCourses()
}

// 点击课程
const handleCourseClick = (course) => {
  router.push(`/learning/course/${course.id}`)
}
</script>

<style scoped>
.courses-container {
  padding: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.card-header h2 {
  margin: 0;
  font-size: 20px;
}

.header-actions {
  display: flex;
  gap: 15px;
}

.course-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 20px;
  margin: 20px 0;
}

.course-card {
  cursor: pointer;
  transition: transform 0.3s;
}

.course-card:hover {
  transform: translateY(-5px);
}

.course-cover {
  position: relative;
  height: 200px;
}

.course-cover .el-image {
  width: 100%;
  height: 100%;
}

.course-tag {
  position: absolute;
  top: 10px;
  right: 10px;
  padding: 2px 8px;
  background-color: var(--el-color-primary);
  color: white;
  border-radius: 4px;
  font-size: 12px;
}

.course-tag.hot {
  background-color: var(--el-color-danger);
}

.course-info {
  padding: 15px;
}

.course-title {
  margin: 0 0 10px;
  font-size: 16px;
  font-weight: bold;
}

.course-desc {
  margin: 0 0 15px;
  color: var(--el-text-color-secondary);
  font-size: 14px;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.course-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 15px;
}

.teacher {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 14px;
}

.stats {
  display: flex;
  gap: 15px;
  color: var(--el-text-color-secondary);
  font-size: 14px;
}

.stats span {
  display: flex;
  align-items: center;
  gap: 5px;
}

.course-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.tags {
  display: flex;
  gap: 5px;
}

.price {
  display: flex;
  align-items: baseline;
  gap: 5px;
}

.current-price {
  color: var(--el-color-danger);
  font-size: 18px;
  font-weight: bold;
}

.original-price {
  color: var(--el-text-color-secondary);
  font-size: 14px;
  text-decoration: line-through;
}

.pagination {
  display: flex;
  justify-content: center;
  margin-top: 20px;
}
</style> 